package netty07.sever;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import netty07.common.codc.RequestDecoder;
import netty07.common.codc.ResponseEncoder;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

/**
 * netty服务端入门
 */
public class Server {

    public static void main(String[] args) {

        //服务类
        ServerBootstrap bootstrap = new ServerBootstrap();

        //boss线程监听端口，worker线程负责数据读写
        ExecutorService boss = Executors.newCachedThreadPool();
        ExecutorService worker = Executors.newCachedThreadPool();

        //设置niosocket工厂
        bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));

        //设置管道的工厂
        bootstrap.setPipelineFactory(() -> {

            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new RequestDecoder());
            pipeline.addLast("encoder", new ResponseEncoder());
            pipeline.addLast("helloHandler", new HelloHandler());
            return pipeline;
        });

        bootstrap.bind(new InetSocketAddress(10101));

        System.out.println("start!!!");

    }

}
